app.service('accountService', function($rootScope, $http, $window, $q, Restangular, $location, $localStorage) {
    this.login = function(account) {

        var deferred = $q.defer();

        $http({
            url: $rootScope.api.account.token,
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            data: 'grant_type=password&client_id=wt&client_secret=123456&username=' + account.username + '&password=' + account.password,
        }).success(function(res) {

            sessionStorage.setItem('accessToken', res.access_token);

            //sessionStorage.setItem('userName', data.userName);
            //$http.defaults.headers.common.Authorization = 'bearer ' + sessionStorage.getItem('accessToken');
            //debugger;
            // var account = $rootScope.account.islogined = true;

            $window.sessionStorage["userInfo"] = JSON.stringify(account);


            deferred.resolve(res);

        }).error(function(err) {

            deferred.reject(err);

        });

        return deferred.promise;

    };


    return {
        login: this.login
    };
});
